<?php

namespace QL\plugins;

use helper\Utils;
use QL\QueryList;

class CollectGoods
{
    /**
     * 采集淘宝商品
     * @param $url //url地址
     * @return array
     */
    static public function taobao($url)
    {
        if (strpos($url, 'item.taobao.com') === false) return [];
        $html = file_get_contents($url);
        $html = iconv('GBK', 'UTF-8//IGNORE', $html);
        $rule = [
            'goods_name' => ["h3.tb-main-title", "text"],
            'thumb' => ["#J_ImgBooth", "src", '', function ($val) {
                return 'http:' . $val;
            }],
            'price' => ["#J_StrPrice>.tb-rmb-num", "text"]
        ];
        $ql   = QueryList::Query($html, $rule);
        $data = $ql->data[0];
        if(strpos($data['price'], '-') !== false){
            $price = explode(' - ',$data['price']);
            $data['price'] = $price[0];
        }
        //获取详情
        $detailUrl      = 'http://dscnew.taobao.com/' . self::cut('\'//dscnew.taobao.com/', '\' :', $ql->html);
        $detailContent  = file_get_contents($detailUrl);
        $detailContent  = iconv('GBK', 'UTF-8//IGNORE', $detailContent);
        $detailContent  = str_replace('var desc=\'', '', $detailContent);
        $data['content'] = stripslashes(substr($detailContent, 0, -3));
        if(!empty($data['thumb'])){ //远程保存到本地
            $ret = Utils::saveImage($data['thumb']);
            $data['thumb'] = $ret['save_path'];
        }
        return $data;
    }

    /**
     * 采集天猫商品
     * @param $url //url地址
     * @return array
     */
    static public function tmall($url)
    {
        if (strpos($url, 'detail.tmall.com') === false) return [];
        $html = file_get_contents($url);
        $html = iconv('GBK', 'UTF-8//IGNORE', $html);
        $rule = [
            'goods_name' => ["input[name='title']", "value"],
            'thumb' => ["#J_ImgBooth", "src", '', function ($val) {
                return 'http:' . $val;
            }],
            'price' => ["#J_StrPrice>.tb-rmb-num", "text"],
        ];
        $ql   = QueryList::Query($html, $rule);
        $data = $ql->data[0];
        //获取价格
        preg_match('/.+"price":"(.+)",.+/U', $ql->html, $price);
        $data['price'] = 0;
        if(isset($price[1])){
            $data['price'] = $price[1];
            if(strpos($data['price'], '-') !== false){
                $price = explode(' - ',$data['price']);
                $data['price'] = $price[0];
            }
        }
        //获取详情
        $detailUrl = 'http:' . self::cut('"api":{"descUrl":"', '","httpsDescUrl"', $ql->html);
        $detailContent  = file_get_contents($detailUrl);
        $detailContent  = iconv('GBK', 'UTF-8//IGNORE', $detailContent);
        $detailContent  = str_replace('var desc=\'', '', $detailContent);
        $data['content'] = stripslashes(substr($detailContent, 0, -3));
        if(!empty($data['thumb'])){ //远程保存到本地
            $ret = Utils::saveImage($data['thumb']);
            $data['thumb'] = $ret['save_path'];
        }
        return $data;
    }

    /**
     * 截取两个字符串之间数据
     * @param $begin
     * @param $end
     * @param $str
     * @return false|string
     */
    static public function cut($begin,$end,$str){
        $b = mb_strpos($str,$begin) + mb_strlen($begin);
        $e = mb_strpos($str,$end) - $b;
        return mb_substr($str,$b,$e);
    }

}